/**
 * Created by LiuJie on 2017/10/11.
 */
var express = require("express");
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var util = require('../../util/commonutil');
var router = express.Router();
var resultJSON = require('../common/resultJSON');
var auth = require('../../auth/auth');
var _deviceList = [
    {
        "name":"测试",
        "parentName":"停车",
        "level":1001
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    },
    {
        "name":"测试",
        "parentName":"行驶",
        "level":1002
    }
];


router.get("/",function (req, res, next) {
    res.render("dataManage/dataManage",{parentMenuList:false,sysMap:false})
});

router.post("/queryUserList",function (req, res, next) {
    var jsonResult = {
        total:0,
        rows:[]
    };
    var sid=auth.getSessionID(req, res, next);
    var qs=req.body;
    // qs["deptid"] = 1;
    qs.sid=sid;
    request({
        url:pro.devServeraddr + "heb/sysDept/list",
        method: "POST",
        qs:qs
    },function (error, response, body) {
        console.log("用户列表数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);

            if(result.success == true){
                console.log("rows:"+result.rows.length);
                jsonResult.total = result.total;
                jsonResult.rows = result.rows;
            }else{
                jsonResult.isSuccess=false;
                jsonResult.message="查询数据异常，请稍后重试！"
            }
        }else{
            jsonResult.isSuccess=false;
            jsonResult.message="查询数据异常，请稍后重试！"
        }
        res.json(jsonResult);
    });
  //res.json(_deviceList)
});

router.post("/refDept",function (req, res, next) {
    var jsonResult = {
        total:0,
        rows:[]
    };
    var sid=auth.getSessionID(req, res, next);
    var qs=req.body;
    // qs["deptid"] = 1;
    qs.sid=sid;
    request({
        url:pro.devServeraddr + "heb/sysDept/refDept",
        method: "POST",
        qs:qs
    },function (error, response, body) {
        //console.log("用户列表数据：>>"+body);
        // if(!error && response.statusCode == 200){
        //     var result = JSON.parse(body);
        //
        //     if(result.success == true){
        //         console.log("rows:"+result.rows.length);
        //         jsonResult.total = result.total;
        //         jsonResult.rows = result.rows;
        //     }else{
        //         jsonResult.isSuccess=false;
        //         jsonResult.message="查询数据异常，请稍后重试！"
        //     }
        // }else{
        //     jsonResult.isSuccess=false;
        //     jsonResult.message="查询数据异常，请稍后重试！"
        // }
        res.json(jsonResult);
    });
    //res.json(_deviceList)
});
//跳转添加用户信息
router.get("/addUserInfo",function (req,res,next) {
   res.render("dataManage/addUserData");
});
router.get("/viewAccountPage",function (req,res,next) {
   res.render("dataManage/dataManage")
});
router.post("/deleteUser",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/sysDept/delete",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("快速增加设备数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="删除数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="删除数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});

module.exports = router;